<template>
	<div id="order" class="container">
		<!-- <div class="search">
      <el-form ref="searchForm" :model="searchForm" inline label-width="90px">
        <el-form-item label="内链名称：" prop="title">
          <el-input v-model="searchForm.title" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search(searchForm)">查询</el-button>
          <el-button @click="reset(searchForm)">重置</el-button>
        </el-form-item>
      </el-form>
    </div> -->
		<h3 class="title"><span />询盘收件箱列表</h3>
		<el-button
			type="primary"
			class="add"
			size="small"
			icon="el-icon-plus"
			style="margin-bottom: 15px"
			@click="goAdd"
			>添加</el-button
		>
		<BouncedDialog
			:title="addForm.id ? '编辑' : '添加'"
			:dialog-show.sync="dialogShow"
			width="50%"
			class="link"
		>
			<el-form
				ref="addForm"
				:model="addForm"
				:rules="rules"
				inline
				class="addForm"
				label-width="80px"
			>
				<el-form-item label="邮箱" prop="email">
					<el-input v-model="addForm.email" />
				</el-form-item>
				<!-- <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password" type="password" />
        </el-form-item> -->
				<el-form-item label="选择站点" prop="website_id">
					<el-select
						v-model="addForm.website_id"
						multiple
						placeholder="请选择站点"
						@change="chooseWedsite"
					>
						<el-option
							v-for="item in options"
							:key="item.id"
							:label="item.domain"
							:value="item.id"
						/>
					</el-select>
				</el-form-item>
				<el-form-item label="状态">
					<el-select v-model="addForm.status">
						<el-option label="正常" :value="1" />
						<el-option label="禁用" :value="2" />
					</el-select>
				</el-form-item>
				<div class="btn">
					<el-button @click="dialogShow = false">取 消</el-button>
					<el-button type="primary" @click="submit()">确 定</el-button>
				</div>
			</el-form>
		</BouncedDialog>
		<el-table
			:data="orderData"
			border
			stripe
			:header-cell-style="{ 'text-align': 'center' }"
			:cell-style="{ 'text-align': 'center' }"
		>
			<el-table-column label="序号" type="index" width="60px" />
			<el-table-column label="邮箱" prop="email" />
			<el-table-column label="状态" prop="status">
				<template slot-scope="scope">
					<el-switch
						v-model="scope.row.status"
						:active-value="1"
						:inactive-value="2"
						active-text="正常"
						inactive-text="禁用"
						active-color="#f84311"
						inactive-color="rgba(0,0,0,0.2)"
						@change="disableUser(scope.row)"
					/>
				</template>
			</el-table-column>
			<el-table-column label="操作" width="150px" fixed="right">
				<template slot-scope="scope">
					<el-button size="mini" class="edit" @click="editor(scope.row)">编辑</el-button>
					<el-button size="mini" class="dele" @click="del(scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			:current-page="page.currentPage"
			:page-sizes="page.pageSizes"
			:page-size="page.size"
			layout="total, sizes, prev, pager, next, jumper"
			:total="page.total"
			:hide-on-single-page="page.total < page.size ? true : false"
			@size-change="handleSizeChange"
			@current-change="handleCurrentChange"
		/>
	</div>
</template>
<script>
import inquiryApi from '@/api/inquiry.js'
import websiteApi from '@/api/website.js'
export default {
	data() {
		return {
			orderData: [], //
			dialogShow: false,
			options: [], // 站点下拉选项
			value: '', // 站点下拉框的值
			addForm: {
				website_id: '',
				email: '',
			},
			rules: {
				email: [{ required: 'true', message: '请输入邮箱', trigger: 'blur' }],
				website_id: [{ required: 'true', message: '请选择站点', trigger: 'change' }],
			},
			searchForm: {
				title: '',
			},
			page: {
				total: 0,
				currentPage: 1,
				size: 10,
				pageSizes: [10, 20, 30, 40],
			}, // 分页器
			val: {},
			id: '',
		}
	},
	computed: {
		siteId1() {
			return this.$store.getters.siteId
		},
	},
	watch: {
		siteId1(val, value) {
			this.siteId = this.$store.getters.siteId
			// this.addForm.website_id = val
			this.getList()
		},
	},
	created() {
		this.siteId = this.$store.getters.siteId
		if (this.siteId) {
			this.getList()
		}

		this.getWebsiteList()
	},
	mounted() {
		// this.getTikets()
	},
	updated() {},
	methods: {
		disableUser(row) {
			const val = {
				id: row.id,
				email: row.email,
				website_id: row.website_id,
				status: row.status,
			}
			inquiryApi.emailUpdate(val).then((res) => {
				if (res.code == 0) {
					this.$message.success(res.msg)
				} else {
					this.$message.error(res.msg)
				}
			})
		},
		// 获取站点列表
		getWebsiteList() {
			websiteApi.websiteList().then((res) => {
				this.options = res.data
			})
		},
		// 选择站点
		chooseWedsite(val) {
			this.addForm.website_id = val
			// this.$forceUpdate()
			//   this.addForm.id = val
		},
		// 新增
		goAdd() {
			this.addForm = {
				email: '',
				website_id: [this.siteId],
				id: '',
				status: 1,
			}
			this.dialogShow = true
		},
		// 编辑
		editor(row) {
			this.addForm = {
				email: row.email,
				website_id: row.website_id,
				id: row.id,
				status: row.status,
			}
			this.dialogShow = true
			//   this.getInfo(row.id)
		},
		// 获取收件详情
		// getInfo(val) {
		//   inquiryApi.inquiryInfo({ id: val }).then(res => {
		//     this.addForm = res.data
		//     this.dialogShow = true
		//   })
		// },
		submit() {
			this.$refs.addForm.validate((valid) => {
				if (valid) {
					if (!this.addForm.id) {
						inquiryApi.emailSave({ ...this.addForm }).then((res) => {
							if (res.code == 0) {
								this.$message.success(res.msg)
								this.dialogShow = false
								this.getList()
							} else {
								this.$message.error(res.msg)
							}
						})
					} else {
						inquiryApi.emailUpdate({ ...this.addForm }).then((res) => {
							if (res.code == 0) {
								this.$message.success(res.msg)
								this.dialogShow = false
								this.getList()
							} else {
								this.$message.error(res.msg)
							}
						})
					}
				} else {
					this.$message.error('请填写必要信息')
				}
			})
		},

		// 删除
		del(row) {
			this.$confirm('此操作将永久删除该收件信息, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					inquiryApi.emailDelete({ id: row.id }).then((res) => {
						if (res.code == 0) {
							this.$message.success('已删除')
							this.getList()
						} else {
							this.$message.error('删除失败')
						}
					})
				})
				.catch(() => {
					this.$message({
						type: 'info',
						message: '已取消删除',
					})
				})
		},
		// 邮箱列表
		getList() {
			this.val.page = this.page.currentPage
			this.val.limit = this.page.size
			// this.val.title = this.searchForm.title
			this.val.website_id = this.siteId
			inquiryApi.emailList(this.val).then((res) => {
				this.orderData = res.data
				this.page.total = res.count
			})
		},
		// 分页规格改变
		handleSizeChange(val) {
			this.page.size = val
			this.page.currentPage = 1
			this.getList()
		},
		// 分页点击
		handleCurrentChange(val) {
			this.page.currentPage = val
			this.getList()
		},
	},
}
</script>
<style lang="scss">
#order {
	.link {
		.el-select {
			width: 100%;
		}
	}
	.addForm .el-form-item__content {
		width: calc(100% - 120px);
	}
}
</style>
<style scoped lang="scss">
.el-table {
	margin-bottom: 20px;
}
.search {
	min-width: 1350px;
	background-color: #fff;
	padding: 35px 30px 20px;
	border-radius: 5px;
}
.el-form-item {
	width: 100%;
}
</style>
